package cn.wolfcode.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2020/5/6.
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    //查询
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")QueryObject qo){
        model.addAttribute("pageInfo",permissionService.query(qo));
        return "permission/list";
    }
    //编辑,跳转到新增或者修改的jsp
    @RequestMapping("/input")
    public String input(Model model,Long id){
        if(id != null){
            model.addAttribute("permission",permissionService.get(id));
        }
        return "permission/input";
    }
    //保存修改
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission dept){
        if(dept.getId() != null){
            permissionService.update(dept);
        }else {
            permissionService.save(dept);
        }
        return "redirect:/permission/list.do";
    }
    //删除
    @RequestMapping("/delete")
    public String delete(Long id){
        if(id != null){
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do";
    }
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(Long id){
        try {
            permissionService.reload();
            return new JsonResult();
        } catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(false,"加载失败");
    }

}
